package com.luania.todolist2.Activity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.luania.todolist2.Model.TaskModel;
import com.luania.todolist2.R;

import java.util.Date;

/**
 * Created by tyz1201 on 2016/4/1.
 */
public class TaskActivity extends AppCompatActivity {
    private String taskName;
    private String task_name;
    private String tasks;
    boolean isNeedNotify;
    CheckBox cb_task;
    EditText et_task;
    LinearLayout ll_task;
    String notifyTime;
    TextView tv_notify_time;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar4);
        et_task = (EditText) findViewById(R.id.et_task);
        // 找到组件。
        Intent intent = getIntent();
        taskName = intent.getStringExtra("taskName");
        toolbar.setTitle(taskName);
        toolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));
        setSupportActionBar(toolbar);

        ll_task = (LinearLayout) findViewById(R.id.ll_task);
        ll_task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataPickerDialog();
            }
        });
    }

    private void dataPickerDialog() {
        Date nowDate = new Date();

        DatePickerDialog datePickerDialog = new DatePickerDialog(TaskActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        Toast.makeText(TaskActivity.this, year + "-" + (monthOfYear+1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
                        notifyTime = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                        timePickerDialog();
                    }
                }, nowDate.getYear() + 1900, nowDate.getMonth(), nowDate.getDate());

        datePickerDialog.show();
    }

    private void timePickerDialog() {
        Date nowDate = new Date();

        TimePickerDialog timePickerDialog = new TimePickerDialog(TaskActivity.this,
                new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        Toast.makeText(TaskActivity.this, hourOfDay + "-" + minute, Toast.LENGTH_SHORT).show();
                        notifyTime = notifyTime + "-" + hourOfDay + "-" + minute;

                        tv_notify_time = (TextView) findViewById(R.id.tv_notify_time);
                        tv_notify_time.setText(notifyTime);
                    }
                }, nowDate.getHours(), nowDate.getMinutes(), true);
        timePickerDialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        task_name = et_task.getText().toString();
        if (id == R.id.action_add_group1) {
            SharedPreferences sharedPreferences = getSharedPreferences("task_name", MODE_PRIVATE);

            cb_task = (CheckBox) findViewById(R.id.cb_task);
            isNeedNotify = cb_task.isChecked();
            long currenttime = System.currentTimeMillis();

            TaskModel taskModel = new TaskModel(notifyTime, task_name, isNeedNotify, currenttime);
            tasks = taskModel.toString();

            //输入任务信息的序列化

            if (task_name.equals("")) {
                Toast.makeText(TaskActivity.this, "任务名不能为空！", Toast.LENGTH_SHORT).show();
                return true;
            }

            if (isExist()) {
                Toast.makeText(TaskActivity.this, "该任务名已存在！", Toast.LENGTH_SHORT).show();
                return true;
            }

            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(task_name, tasks);
            editor.commit();
            //拿到SharePreference 编辑器对象，进行输入数据存储。
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    boolean isExist() {
        task_name = et_task.getText().toString();
        SharedPreferences sharedPreferences = getSharedPreferences("task_name", MODE_PRIVATE);
        return sharedPreferences.contains(task_name);
    }
}


